package com.zzx.graduation.controller;



import com.zzx.graduation.entity.News;
import com.zzx.graduation.entity.User;
import com.zzx.graduation.response.ResponseMessage;
import com.zzx.graduation.response.StateCode;
import com.zzx.graduation.service.NewsService;
import com.zzx.graduation.service.UserCRUD;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import javax.transaction.Transactional;
import java.util.List;


//@Transactional
@RestController
@CrossOrigin
@Transactional
@RequestMapping("/api/news")
public class NewsController {

    //slf4j.Logger
    private  static final Logger log = LoggerFactory.getLogger(NewsController.class);


    @Autowired
    NewsService newsService;//Service


    /**
     *查询历史消息
     * @param id id 消息人id
     * @param type 类型  0：发消息人   1： 收消息人
     * @param isGroup 是否为群聊id，等于news对象中的targettype，只有type为1的时候此值才可能为1，
     * @return 例如 id=1，type=1表示：id为1的员工所发的所以消息
     *
     * 目前 type 和 isgroup为 0接口开放是安全的
     *
     *
     * 发送消息的只有员工，
     * 当消息发往群聊的时候，消息要中转到kafka，文件存入服务器
     * 消息主题按照群聊，广播到所有人
     *
     */
    @GetMapping("/news/{id}/{type}/{isGroup}")
    @ResponseStatus(HttpStatus.OK)
    public ResponseMessage getNews(@PathVariable("id") Integer id,
                          @PathVariable("type") Integer type,
                          @PathVariable("isGroup") Integer isGroup){

        if(null == id || (type == 0 && isGroup == 1) ||
                ( type != 0 && type != 1) || (isGroup != 0 && isGroup != 1)){
            return  null;
        }
      //查询消息
        List<News> news = null;
        if ( type == 0) {
           news = newsService.findNewsBySourceId(id);
        }else {
           news = newsService.findNewByTargetIdAndType(id, isGroup);
        }

        if (news == null)
        {
            return  new ResponseMessage<List<News>>(StateCode.NOT_FOUNT,"查询失败",news);
        }
        return   new ResponseMessage<List<News>>(StateCode.SUCCESS,"查询成功",news);
    }

    /**
     * 新增一个消息
     * 非幂等
     * */
    @PostMapping("/new")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage addNews(@RequestBody News news){
        if(null == news){
            return  null;
        }
        News addNews = newsService.addNews(news);
        if(addNews == null)
        {
            return  new ResponseMessage<News>(StateCode.NOT_FOUNT,"新增失败",addNews);
        }

        //如果是文件，则在服务器中添加该文件，上传文件成功后由前端推送一个消息给后端，并且把文件url地址写进来

        //并且把消息推送到kafka



        return   new ResponseMessage<News>(StateCode.SUCCESS,"新增成功",addNews);
    }


    /**
     * 编辑一个消息，不对外开放
     * 幂等性
     * */
    @PutMapping("/new/{id}")
    @ResponseStatus(HttpStatus.CREATED)
    public ResponseMessage editNews(@PathVariable("id") Integer id,@RequestBody News news){
        if(null == news || id == null){
            return  null;
        }
        news.setId(id);
        News modifyNews = newsService.modifyNews(news);
        if(modifyNews == null)
        {
            return  new ResponseMessage<News>(StateCode.NOT_FOUNT,"修改失败",modifyNews);
        }

        return   new ResponseMessage<News>(StateCode.SUCCESS,"修改成功",modifyNews);
    }

    /**
     * 删除一个消息
     * 幂等性
     * */
    @DeleteMapping("/new/{id}")
    @ResponseStatus(HttpStatus.NO_CONTENT)
    public ResponseMessage deleteNews(@PathVariable("id") Integer id){
        if(null == id){
            return  null;
        }
        Boolean b = newsService.deleteNews(id);
        //如果是删除文件，则把对应文件删掉，也可以不删



        if(b == null || b == false)
        {
            return  new ResponseMessage<News>(StateCode.NOT_FOUNT,"删除失败",null);
        }

        return   new ResponseMessage<News>(StateCode.SUCCESS,"删除成功",null);
    }


}
